package com.mingqijia.gassafety.webserver.webSocket;

import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.webserver.listeners.OrderAutoGenMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName SubscriptionConfig.java
 * @createTime 2022年06月23日
 */
@Configuration
public class SubscriptionConfig {

	@Bean
	MessageListenerAdapter messageListener() {
		return new MessageListenerAdapter(new WebSocketMessageListener());
	}

	@Bean
	MessageListenerAdapter dpMessageListener() {
		return new MessageListenerAdapter(new WebSocketDpMessageListener());
	}

	@Bean
	MessageListenerAdapter gsMessageListener()  {
		return new MessageListenerAdapter(new WebSocketUserMessageListener());
	}

	@Bean
	MessageListenerAdapter autoGenOrderMessageListener(){
		return new MessageListenerAdapter(new OrderAutoGenMessageListener());
	}


	@Bean
	RedisMessageListenerContainer redisContainer(RedisConnectionFactory factory) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(factory);
		// 支持多个topic
		//驾驶舱
		container.addMessageListener(messageListener(), new ChannelTopic(Constants.TOPIC_WEBSOCKET));
		//集团大屏
		container.addMessageListener(dpMessageListener(), new ChannelTopic(Constants.TOPIC_DP_WEBSOCKET));
		//工商业大屏
		container.addMessageListener(gsMessageListener(), new ChannelTopic(Constants.TOPIC_USER_WEBSOCKET));
		//自动创建工单（非websocket）
		container.addMessageListener(autoGenOrderMessageListener(), new ChannelTopic(Constants.TOPIC_ORDER_AUTO_CREATE));
		return container;
	}
}

